package com.hbnu.tangyalu.jdbc;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcDemo03 {
    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            InputStream resourceAsStream=JdbcDemo02.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties=new Properties();
            properties.load(resourceAsStream);
            String driver=properties.getProperty("driver");
            String url=properties.getProperty("url");
            String user=properties.getProperty("user");
            String password=properties.getProperty("password");



            Class.forName(driver);
            connection= DriverManager.getConnection(url,user,password);
            statement=connection.createStatement();
            String sql="select * from tb_user";
            resultSet=statement.executeQuery(sql);
            while (resultSet.next()){

            }
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
        finally {
//            关闭资源从小到大，一个一个的关闭
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }

            try {
                if (statement!=null){
                    statement.close();
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }

            try {
                if (connection!=null){
                    connection.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

    }
}
